package com.cheer.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cheer.entity.OaAdminEntity;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;


/**
 * 管理员
 *
 * @author cheer
 */
@Mapper
@Repository
public interface OaAdminDao extends BaseMapper<OaAdminEntity> {

    default OaAdminEntity selectByPhone(String phone) {
        return selectOne(new QueryWrapper<OaAdminEntity>()
                .eq("ad_tel", phone)
                .last("limit 1"));
    }

    default IPage<OaAdminEntity> selectPage(Integer pageNum, Integer pageSize, String keyword) {
        QueryWrapper<OaAdminEntity> queryWrapper = new QueryWrapper<>();

        keyword = StringUtils.trimToNull(keyword);
        if (keyword != null) {
            queryWrapper.like("ad_name", keyword)
                    .or()
                    .like("ad_tel", keyword);
        }

        queryWrapper.ne("role_id", 2);

        return selectPage(new Page<>(pageNum, pageSize), queryWrapper);
    }

    default void deleteByPid(Integer pid) {
        delete(new QueryWrapper<OaAdminEntity>()
                .eq("pid", pid));
    }
}
